package cn.xorange.commons.system.controller.system;

import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.SysUser;
import cn.xorange.commons.system.service.System.ISysUserService;
import cn.xorange.commons.system.service.platform.ISysTenantService;
import cn.xorange.commons.utils.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : yangjian
 * date : 2025/1/24
 * description :
 */
@RestController
@Authorize(hasPerm = "system:user")
@RequestMapping("/system/user")
public class SysUserController extends DataController<SysUser, ISysUserService> {

    @Resource
    ISysTenantService tenantService;

    @Override
    @Authorize(hasPerm = "query")
    @GetMapping("/{id}")
    public ResultBean info(@PathVariable(value = "id") Long id){

        List<Long> postIds = this.service.getUserPostIds(id);
        List<Long> roleIds = this.service.getUserRoleIds(id);
        return ResultBean.onSuccess(this.service.getById(id)).put("roleIds", roleIds).put("postIds", postIds);
    }


    @Override
    @Authorize(hasPerm = "add")
    @PostMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean add(@Validated @RequestBody SysUser user){
        if(StringUtils.isEmpty(user.getStaffId()) && StringUtils.isEmpty(user.getPhone())){
            return ResultBean.onFail("编号和手机号不能同时为空");
        }
        Long tenantId = SecurityUtils.getTenantId();
        String initPwd = tenantService.getById(tenantId).getInitPwd();
        String pwd = SecurityUtils.encryptPassword(initPwd);
        user.setPassword(pwd);
        return ResultBean.onSuccess(this.service.add(user));
    }

    @Override
    @Authorize(hasPerm = "edit")
    @PutMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean update(@Validated @RequestBody SysUser user){
        if(StringUtils.isEmpty(user.getStaffId()) && StringUtils.isEmpty(user.getPhone())){
            return ResultBean.onFail("编号和手机号不能同时为空");
        }
        user.setPassword(null);
        return ResultBean.onSuccess(this.service.update(user));
    }

    @Authorize(hasPerm = "authRole")
    @PutMapping("/authRole")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean authRole(@RequestBody SysUser user){
        this.service.setUserRoleIds(user);
        return ResultBean.onSuccess();
    }

    @Authorize(hasPerm = "edit")
    @PutMapping("/resetPwd/{userId}")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean resetPwd(@PathVariable(value = "userId") Long userId){

        Long tenantId = SecurityUtils.getTenantId();
        String initPwd = tenantService.getById(tenantId).getInitPwd();
        String pwd = SecurityUtils.encryptPassword(initPwd);
        SysUser user = new SysUser();
        user.setPassword(pwd);
        user.setUserId(userId);
        return ResultBean.onSuccess(this.service.update(user));
    }


    @Authorize(hasPerm = "remove'")
    @DeleteMapping("/batch/{ids}")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean del(@PathVariable Long[] ids) {
        this.service.deleteByIds(ids);
        return ResultBean.onSuccess();
    }


    @PutMapping("/editPwd")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean editPwd(@RequestBody SysUser user){
        if(StringUtils.isEmpty(user.getPassword())){
            return ResultBean.onFail("密码不能为空");
        }
        String pwd = SecurityUtils.encryptPassword(user.getPassword());
        user.setPassword(pwd);
        Long userId = SecurityUtils.getUserId();
        user.setUserId(userId);
        return ResultBean.onSuccess(this.service.update(user));
    }




}
